package com.zhangtai.modules.controller.admin;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhangtai.common.annotation.SysAuthorityLog;
import com.zhangtai.modules.controller.aouthLogin.LoginController;
import com.zhangtai.modules.dao.*;
//import com.zhangtai.modules.dao.ssqlDao.BpmUserDao;
//import com.zhangtai.modules.dao.ssqlDao.JobDaoMssql;
import com.zhangtai.modules.dto.ContractMainUserDto;
import com.zhangtai.modules.entity.common.IsGroupEntity;
import com.zhangtai.modules.entity.employee.EmployeeInfoEntity;
import com.zhangtai.modules.entity.salary.UserSalaryEntity;
import com.zhangtai.modules.entity.system.*;
import com.zhangtai.modules.jobTask.qw.QwUserEntity;
import com.zhangtai.modules.service.baseFromMssql.BaseMssqlServiceImpl;
import com.zhangtai.modules.service.salary.UserSalaryService;
import com.zhangtai.modules.service.system.ContrarctUserService;
import com.zhangtai.modules.service.system.RoleMenuService;
import com.zhangtai.modules.service.system.UserService;
import com.zhangtai.modules.vo.*;
import com.zhangtai.utils.BeanUtilsMabach;
import com.zhangtai.utils.R;
import com.zhangtai.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.*;

;

@RestController
@Api(tags="权限用户角色接口")
@RequestMapping("roleAssignment")
@Slf4j
public class UserController {

	@Autowired
	private SecurityDao securityDao;

	@Autowired
	private RoleMenuService roleMenuService;

	@Autowired
	private RoleMenuDao roleMenuDao;

	@Autowired
	private UserRoleDao userRoleDao;
	@Autowired
	private UserService userService;
//	@Autowired
//	private JobDaoMssql jobDaoMssql;
	@Autowired
	private RedisTemplate redisTemplate;
	@Autowired
	private EmployeeInfoDao employeeInfoDao;
	@Autowired
	private BaseMssqlServiceImpl commonService;
	@Autowired
	private EmployessController employessController;
//	@Autowired
//	private BpmUserDao bpmUserDao;
	@Autowired
	private QwUserDao qwUserDao;
	@Autowired
	private UserDao userDao;
	@Autowired
	private ContrarctUserDao contrarctUserDao;

	@Autowired
	private IsGroupDao isGroupDao;

	@Autowired
	PasswordEncoder passwordEncoder;

	@Autowired
	private UserSalaryService userSalaryService;

	@Autowired
    ContrarctUserService contrarctUserService;

	@Autowired
	ContractSubGroupDao contractSubGroupDao;

	@Autowired
	ContractGroupDao contractGroupDao;

	@Autowired
	LoginController loginController;
	@Autowired
	private JobController jobController;

	@Autowired
	EmployerCompanyDao employerCompanyDao;
    private String contractPermission;

	@Autowired
	private UserSalaryDao userSalaryDao;


//	/**
//	 * 修改用户公司组
//	 * @param userUpdateVo
//	 * @return
//	 */
//	@PostMapping("/updateUserGroup")
//	@ApiOperation(value="修改用户公司组")
//	public R updateUser(@RequestBody @Valid UserUpdateVo userUpdateVo){
//		UserEntity userEntity = BeanUtilsMabach.doToDto(userUpdateVo, UserEntity.class);
//		R r = userService.updateByIdE(userEntity);
//		//更新用户公司组信息
//		loginController.updateUsrOrgPermission(userEntity.getUserId());
//		return r;
//
//
//	}

	@PostMapping("/saveUserRole")
    @ApiOperation(value="保存用户角色中间表")
	@SysAuthorityLog(value = "保存用户角色中间表")
    @Transactional
	@PreAuthorize("hasAuthority('sys:admin:update')")
	public R saveUserRole(@RequestBody @Valid UserRoleVo userRoleVo) throws Exception {
//		 JSONObject  user = loginController.getUserInfo(request).getData();
//		 Long userId = Long.parseLong(user.getString("id"));
		 //集合不为空
//		 if(!CollectionUtils.isEmpty(UserRole)) {
//			 for(UserRoleEntity item:UserRole) {
//				 UserRoleEntity userRole = new UserRoleEntity();
//				 userRole.setUserId(userId);
//				 userRole.setRoleId(item.getRoleId());
//				 //保存
//				 userRoleService.saveE(userRole);
//			 }
//		 }
		Long userId = userRoleVo.getUserId();
		List<Long> roleIds = userRoleVo.getRoleIds();
		if (CollectionUtils.isEmpty(roleIds)){
			return R.error("角色id不能为空");
		}
		for (Long roleId : roleIds) {
			UserRoleEntity userRoleEntity = new UserRoleEntity();
			userRoleEntity.setUserId(userId);
			userRoleEntity.setRoleId(roleId);
			userRoleDao.insert(userRoleEntity);


		}
		//			删除缓存中的登录状态，使权限重新生效
		UserEntity userEntity = userService.getByIdE(userId);
		String token = userEntity.getToken();
		redisTemplate.delete(token);

		return R.ok();
	}



	@PostMapping("/updateByIdE")
    @ApiOperation(value="编辑用户角色中间表")
	@SysAuthorityLog(value = "编辑用户角色中间表")
    @Transactional
	@PreAuthorize("hasAuthority('sys:admin:update')")
	public R updateByIdE(@RequestBody @Valid UserRoleUpdate userRoleUpdate) throws Exception {
//		JSONObject  user = loginController.getUserInfo(request).getData();
//		 Long userId = Long.parseLong(user.getString("id"));
//		 //先删除原有的数据
//		 userRoleDao.deleteUserRole(userId);
//		 //集合不为空
//		 if(!CollectionUtils.isEmpty(UserRole)) {
//			 for(UserRoleEntity item:UserRole) {
//				 UserRoleEntity userRole = new UserRoleEntity();
//				 userRole.setUserId(userId);
//				 userRole.setRoleId(item.getRoleId());
//				 //保存
//				 userRoleService.saveE(userRole);
//			 }
//		 }
		UserRoleEntity userRoleEntity = BeanUtilsMabach.doToDto(userRoleUpdate, UserRoleEntity.class);
		userRoleDao.updateById(userRoleEntity);
		//			删除缓存中的登录状态，使权限重新生效
		UserEntity userEntity = userService.getByIdE(userRoleUpdate.getUserId());
		String token = userEntity.getToken();
		redisTemplate.delete(token);


		return R.ok();
	}

	@DeleteMapping("/deleteUserRole")
    @ApiOperation(value="删除用户角色中间表")
	@SysAuthorityLog(value = "删除用户角色中间表")
    @Transactional
	@PreAuthorize("hasAuthority('sys:admin:update')")
	public R deleteUserRole(Long userId) {
		//删除
		int i = userRoleDao.deleteById(userId);
		if (i>0){
			//			删除缓存中的登录状态，使权限重新生效
			UserEntity userEntity = userService.getByIdE(userId);
			String token = userEntity.getToken();
			redisTemplate.delete(token);
			return R.ok();
		}else {
			return R.error();
		}

	}


	@PostMapping("/saveRoleMenu")
	@ApiOperation(value="保存角色-资源中间表")
	@SysAuthorityLog(value = "保存角色-资源中间表")
	@Transactional
	@PreAuthorize("hasAuthority('sys:admin:update')")
	public R saveRoleMenu(@RequestBody @Valid RoleMenuVo roleMenuVo) {
		String[] menuIds = roleMenuVo.getMenuIds();
		Long roleId = roleMenuVo.getRoleId();
		if(menuIds.length != 0) {
			for(String menuId :menuIds) {
				RoleMenuEntity roleMenuEntity = new RoleMenuEntity();
				roleMenuEntity.setRoleId(roleId);
				roleMenuEntity.setMenuId(Long.parseLong(menuId));
				//保存
				roleMenuService.saveE(roleMenuEntity);
			}

		}
		//			删除缓存中的登录状态，使权限重新生效

		List<UserRoleEntity> userRoleEntities = userRoleDao.selectList(new QueryWrapper<UserRoleEntity>().eq("role_id", roleId));
		if (CollectionUtils.isEmpty(userRoleEntities)){
			for (UserRoleEntity userRoleEntity : userRoleEntities) {
				Long userId = userRoleEntity.getUserId();
				UserEntity userEntity = userService.getByIdE(userId);
				String token = userEntity.getToken();
				redisTemplate.delete(token);
			}
		}
		return R.ok();
	}

	@DeleteMapping("/dateleRoleMenu/{id}")
	@ApiOperation(value="删除角色-权限中间表")
	@SysAuthorityLog(value = "删除角色-权限中间表")
	@Transactional
	@PreAuthorize("hasAuthority('sys:admin:update')")
	public R deleteRoleMenu(@PathVariable("id") Long id) {
		int i = roleMenuDao.deleteById(id);
		if (1>0){
			//			删除缓存中的登录状态，使权限重新生效

			List<UserRoleEntity> userRoleEntities = userRoleDao.selectList(new QueryWrapper<UserRoleEntity>().eq("role_id", id));
			if (CollectionUtils.isEmpty(userRoleEntities)){
				for (UserRoleEntity userRoleEntity : userRoleEntities) {
					Long userId = userRoleEntity.getUserId();
					UserEntity userEntity = userService.getByIdE(userId);
					String token = userEntity.getToken();
					redisTemplate.delete(token);
				}
			}
			return R.ok();
		}else {
			return R.error();
		}

	}


/*
	@PostMapping("/updateRoleMenu")
	@ApiOperation(value="更新角色菜单-菜单间表")
	@Transactional
	@PreAuthorize("hasAuthority('sys:update')")
	public R updateRoleMenu(@RequestParam(value = "roleId", required = false) Long roleId,
							@RequestParam(value = "menuIds", required = false) List<RoleMenuEntity> menuIds) {

		roleMenuDao.deleteRoleMenuAndAuthority(roleId,1);
		if(!CollectionUtils.isEmpty(menuIds)) {
			for(RoleMenuEntity item:menuIds) {
				RoleMenuEntity roleMenuEntity = new RoleMenuEntity();
				roleMenuEntity.setRoleId(roleId);
				roleMenuEntity.setMenuId(item.getMenuId());
				//保存
				roleMenuService.saveE(roleMenuEntity);
			}
		}
		//			删除缓存中的登录状态，使权限重新生效

		List<UserRoleEntity> userRoleEntities = userRoleDao.selectList(new QueryWrapper<UserRoleEntity>().eq("role_id", roleId));
		if (CollectionUtils.isEmpty(userRoleEntities)){
			for (UserRoleEntity userRoleEntity : userRoleEntities) {
				Long userId = userRoleEntity.getUserId();
				UserEntity userEntity = userService.getByIdE(userId);
				String token = userEntity.getToken();
				redisTemplate.delete(token);
			}
		}
		return R.ok();
	}

	@PostMapping("/updateRoleAuthority")
	@ApiOperation(value="更新角色权限-权中间表")
	@Transactional
	@PreAuthorize("hasAuthority('sys:update')")
	public R updateRoleAuthority(@RequestParam(value = "roleId", required = false) Long roleId,
							@RequestParam(value = "menuIds", required = false) List<RoleMenuEntity> menuIds) {
		//先删除原来的数据
		roleMenuDao.deleteRoleMenuAndAuthority(roleId,2);
		if(!CollectionUtils.isEmpty(menuIds)) {
			for(RoleMenuEntity item:menuIds) {
				RoleMenuEntity roleMenuEntity = new RoleMenuEntity();
				roleMenuEntity.setRoleId(roleId);
				roleMenuEntity.setMenuId(item.getMenuId());
				//保存
				roleMenuService.saveE(roleMenuEntity);
			}
		}
		//			删除缓存中的登录状态，使权限重新生效

		List<UserRoleEntity> userRoleEntities = userRoleDao.selectList(new QueryWrapper<UserRoleEntity>().eq("role_id", roleId));
		if (CollectionUtils.isEmpty(userRoleEntities)){
			for (UserRoleEntity userRoleEntity : userRoleEntities) {
				Long userId = userRoleEntity.getUserId();
				UserEntity userEntity = userService.getByIdE(userId);
				String token = userEntity.getToken();
				redisTemplate.delete(token);
			}
		}
		return R.ok();
	}
*/

	/**
	 * 获取部门下的用户
	 * @param orgId
	 * @return
	 */
	@GetMapping("/getDepartmentUser")
	@ApiOperation(value="获取部门下的用户")
	public R getDepartmentUser(Long orgId,Boolean isDismiss){
//		jobDaoMssql.getDepartmentUser(orgId,isDismiss);
		List list = new ArrayList();
		List<EmployeeInfoEntity> employeeInfoEntityList = employeeInfoDao.selectList(new QueryWrapper<EmployeeInfoEntity>().eq("unit_id",orgId).ne(!isDismiss,"employeestatus",3).eq("is_delete",0));
		List<Map<String,Object>> reList = new ArrayList<>();
		Map<String,Object> reMap = new HashMap<>();
		for(EmployeeInfoEntity entity:employeeInfoEntityList){
			Map<String,Object> map = new HashMap<>();
			map.put("oid",entity.getOid().toString());
			map.put("name",entity.getName());
			map.put("code",entity.getCode());
			reList.add(map);
			reMap.put(entity.getCode(),map);
		}
		for(int i=0;i<list.size();i++){
			Map map = (Map) list.get(i);
			String userCode = map.get("code").toString();
			//查看当前部门人员是否存在本地库
			EmployeeInfoEntity employeeInfoEntity = employeeInfoDao.selectOne(new QueryWrapper<EmployeeInfoEntity>().eq("code", userCode).eq("is_delete", 0));
			//本地库不存在，插入数据
			if (ObjectUtils.isEmpty(employeeInfoEntity)){
				commonService.finEmpFromMssqlByCode(userCode);
			}
			if(ObjectUtils.isEmpty(reMap.get(userCode))){
				reList.add(map);
			}
		}

		return R.ok(reList);
	}

	@GetMapping("/getBpmUserByCode")
	@ApiOperation(value="根据code 查找员工信息添加user信息")
	@SysAuthorityLog(value = "根据工号添加用户信息")
	public R getEmployessInfoByCode(@RequestParam("codes") String codes) {
		  String[] codeStr=	codes.split(",");
		  String existStr="已存在user表员工工号：";
		  String notFoundStr="根据员工工号未找到：";
		  String successStr="成功添加user表员工工号：";
		  for (String code:codeStr){
				UserEntity userEntity=	userDao.selectOne(new QueryWrapper<UserEntity>().eq("usercode", code).eq("is_delete",0));
				//boolean flag=	passwordEncoder.matches("123456",userEntity.getPassword());
				if(!ObjectUtils.isEmpty(userEntity)){
					existStr+=code+",";
					continue;
				}
//				userEntity=  bpmUserDao.findBpmUserByUserCode(code);
				if(ObjectUtils.isEmpty(userEntity)){
					notFoundStr+=code+",";
					continue;
				}
				userEntity.setCreateTime(new Date());
				userEntity.setPassword(passwordEncoder.encode("123456"));
				List<QwUserEntity> qwUserEntitys = qwUserDao.selectList(new QueryWrapper<QwUserEntity>().eq("mobile", userEntity.getMobile()).or().eq("user_code", code));
				if (!CollectionUtils.isEmpty(qwUserEntitys)){
					QwUserEntity qwUserEntity = qwUserEntitys.get(0);
					userEntity.setQwAccount(qwUserEntity.getUserid());
				}
				userService.saveE(userEntity);
			  successStr+=code+",";
		  }
		JSONObject object=new JSONObject();
		object.put("existStr",existStr);
		object.put("notFoundStr",notFoundStr);
		object.put("successStr",successStr);
		return R.ok(object);
	}

	@DeleteMapping("/deleteUserById")
	@ApiOperation(value="根据id删除用户")
	@SysAuthorityLog(value = "根据id删除用户")
	@Transactional
	public R deleteUserById(@RequestParam("userId") String userId) {
			userDao.deleteById(Long.valueOf(userId));
		return R.ok();
	}

	@PostMapping("/updateUserPwdByUsername")
	@ApiOperation(value="根据Username修改用户密码")
	@SysAuthorityLog(value = "根据Username修改用户密码")
	@Transactional
	public R deleteUserById(@RequestParam("username") String username,@RequestParam("oidPwd") String oidPwd,@RequestParam("newPwd") String newPwd) {
		UserEntity userEntity=	userDao.selectOne(new QueryWrapper<UserEntity>().eq("username", username).eq("is_delete",0));
		if(ObjectUtils.isEmpty(userEntity)){
			R.error("用户不存在");
		}
		if(!StringUtils.isEmpty(userEntity.getPassword())){
			if(!passwordEncoder.matches(oidPwd,userEntity.getPassword())){
				return R.error("登录密码输入错误！");
			}
		}
		userEntity.setPassword(passwordEncoder.encode(newPwd));
		userDao.updateById(userEntity);
		return R.ok();
	}


	@PostMapping("/updateUserPassword")
	@ApiOperation(value="根据id重置密码")
	@SysAuthorityLog(value = "根据id重置密码")
	@Transactional
	public R updateUserPassword(@RequestParam("id") String id) {
		UserEntity userEntity=	userDao.selectOne(new QueryWrapper<UserEntity>().eq("user_id", id).eq("is_delete",0));
		if(ObjectUtils.isEmpty(userEntity)){
			R.error("用户不存在");
		}
		
		userEntity.setPassword(passwordEncoder.encode("123456"));
		userDao.updateById(userEntity);
		return R.ok();
	}



	@GetMapping("/updateUserStatus")
	@ApiOperation(value = "根据id修改状态")
	@SysAuthorityLog(value = "根据id修改状态")
	@Transactional
	public R updateUserStatus(@RequestParam("userId") String userId,@RequestParam("status") Integer status){
		UserEntity entity=new UserEntity();
		entity.setUserId(Long.valueOf(userId));
		entity.setStatus(status);
		return userService.updateByIdE(entity);
	}


	@GetMapping("/getContractAllMainIds")
	@ApiOperation(value = "查询-查询所有的合同主体")
	@Transactional
	public R getContractAllMainIds() throws Exception {


		List<ContractGroupEntity> 	list = contractGroupDao.selectList(null);

		ArrayList<ContractMainUserDto> dtos = new ArrayList<ContractMainUserDto>();
		if(!CollectionUtils.isEmpty(list)) {
			for ( ContractGroupEntity item:list
					){
				ContractMainUserDto dto = new ContractMainUserDto();
				if(item.getId()!=null) {
					dto.setId(item.getId().toString());
				}
				if(item.getGroupName()!=null) {
					dto.setContractMainName(item.getGroupName());
				}
				dtos.add(dto);
			}
		}
		return R.ok(dtos);
	}


	@GetMapping("/getContractMainIds")
	@ApiOperation(value = "查询-根据用户id权限查询已保存的合同主体")
	@Transactional
	public R getContractMainIds(@RequestParam("userId") String userId) throws Exception {
		//获取当前用户角色下合同主体Id
        //获取缓存中的合同主体组id
        List<Long> contractGoupIds= (List<Long>) redisTemplate.boundValueOps("contractPermission"+userId).get();

		if(CollectionUtils.isEmpty(contractGoupIds)){
			List<ContrarctUserEntity> contrarctUserEntityList= contrarctUserDao.selectList(new QueryWrapper<ContrarctUserEntity>()
					.eq("user_id",userId));
			if(!CollectionUtils.isEmpty(contrarctUserEntityList)){
				contractGoupIds=new ArrayList<>();
				for (ContrarctUserEntity contrarctUserEntity:contrarctUserEntityList){
					contractGoupIds.add(contrarctUserEntity.getContractMainId());
				}
			}
		}

        List<ContractGroupEntity> list=null;
        if(!CollectionUtils.isEmpty(contractGoupIds)) {
           list = contractGroupDao.selectBatchIds(contractGoupIds);
        }
		ArrayList<ContractMainUserDto> dtos = new ArrayList<ContractMainUserDto>();
		if(!CollectionUtils.isEmpty(list)) {
			for ( ContractGroupEntity item:list
			 ){
				ContractMainUserDto dto = new ContractMainUserDto();
				if(item.getId()!=null) {
					dto.setId(item.getId().toString());
				}
				if(item.getGroupName()!=null) {
					dto.setContractMainName(item.getGroupName());
				}
				dtos.add(dto);
			}
		}
		return R.ok(dtos);
	}

	@PostMapping("/saveContratctUser")
	@ApiOperation(value = "保存-分配合同主体组id集合和用户id")
	@SysAuthorityLog(value = "保存-分配合同主体组id集合和用户id")
	public R saveContractUser(@RequestBody UserContractVo userContractVo){

		if(!StringUtils.isEmpty(userContractVo.getUserId())){
			contrarctUserDao.delete(new QueryWrapper<ContrarctUserEntity>().eq("user_id",userContractVo.getUserId()));
		}

	    if(!CollectionUtils.isEmpty(userContractVo.getContractMainIds())){

            for (String item:userContractVo.getContractMainIds()
                 ) {
                ContrarctUserEntity cu=new ContrarctUserEntity();
                cu.setUserId(new Long(userContractVo.getUserId()));
                cu.setContractMainId(new Long(item.toString()));
                contrarctUserService.saveE(cu);
            }
            //存到redis
            redisTemplate.boundValueOps("contractPermission"+userContractVo.getUserId()).set(userContractVo.getContractMainIds());
        }else{
	    	contrarctUserDao.deleteContractUser(userContractVo.getUserId());
			redisTemplate.boundValueOps("contractPermission"+userContractVo.getUserId()).set(null);

		}

		return  R.ok();
	}

	@GetMapping("/getSalaryByUserId")
	@ApiOperation(value = "根据用户id权限查询已保存的薪资组")
	public R getSalaryByUserId(@RequestParam("userId") String userId) throws Exception {
		//获取当前用户角色下合同主体Id
		//获取缓存中的公司信息
		List<String> salaryIds= (List<String>) redisTemplate.boundValueOps("SalaryPermission"+userId).get();
//		if(!CollectionUtils.isEmpty(salaryIds)) {

//		List<SalaryGroupDto> salaryGroupDtos = jobDaoMssql.findAllsalaryGroupFromSsqlAuth(salaryIds);
//		if(!CollectionUtils.isEmpty(salaryGroupDtos)) {
//			return R.ok(salaryGroupDtos);
//			}
//			return R.ok(salaryIds);

//		}
		if(CollectionUtils.isEmpty(salaryIds)){
			List<UserSalaryEntity> userSalaryEntityList= userSalaryDao.selectList(new QueryWrapper<UserSalaryEntity>()
					.eq("user_id",userId));
			if(!CollectionUtils.isEmpty(userSalaryEntityList)){
				salaryIds=new ArrayList<>();
				for (UserSalaryEntity userSalaryEntity:userSalaryEntityList){
					salaryIds.add(userSalaryEntity.getSalaryId()+"");
				}
				HashSet set = new HashSet(salaryIds);
				//把List集合所有元素清空
				salaryIds.clear();
				//把HashSet对象添加至List集合
				salaryIds.addAll(set);
			}
		}

		return R.ok(salaryIds);
	}

	private void salaryGroupDtos() {
	}

	@PostMapping("/saveSalaryUser")
	@ApiOperation(value = "保存-分配薪资id集合和用户id")
	@SysAuthorityLog(value = "保存-分配薪资id集合和用户id")
	public R saveSalaryUser(@RequestBody UserSalaryVo userSalaryVo,HttpServletRequest request) throws Exception {

		if(!StringUtils.isEmpty(userSalaryVo.getUserId())){
			userSalaryDao.delete(new QueryWrapper<UserSalaryEntity>().eq("user_id",userSalaryVo.getUserId()));
		}
		if(!CollectionUtils.isEmpty(userSalaryVo.getOrgIds())){

			R uR = loginController.getUserInfo(request);
			JSONObject user = (JSONObject) uR.getData();
			String userId = user.getString("id");
			//存到redis
			//华东的 和测试的映射在一起了，碰见华东的改成测试的
			List<String> listOrgIds=userSalaryVo.getOrgIds();
			ArrayList<String> newOrgIds = new ArrayList<>();
			for (String orgId:listOrgIds
				 ) {
				if(orgId.equals("68290407") || orgId.equals("68290553")){
					orgId="68747780";
					newOrgIds.add(orgId);
				}else {
					newOrgIds.add(orgId);
				}
			}
			userSalaryVo.setOrgIds(newOrgIds);

			for (String item:userSalaryVo.getOrgIds()
			) {
				UserSalaryEntity cu=new UserSalaryEntity();
				cu.setUserId(new Long(userSalaryVo.getUserId()));
				cu.setSalaryId(new Long(item.toString()));
				cu.setIsDelete(0);
				cu.setOperationId(new Long(userId));
				userSalaryService.saveE(cu);
			}

			redisTemplate.boundValueOps("SalaryPermission"+userSalaryVo.getUserId()).set(userSalaryVo.getOrgIds());
		}else {
			redisTemplate.boundValueOps("SalaryPermission"+userSalaryVo.getUserId()).set(null);
		}

		return  R.ok();
	}


	@GetMapping("/saveIsGroup")
	@ApiOperation(value = "保存是否集团公司数据")
	@SysAuthorityLog(value = "保存是否集团公司数据")
	@Transactional
	public R saveSalaryUser(@RequestParam List<Long> isGroupArrayList) throws Exception {

		isGroupDao.delete(new QueryWrapper<IsGroupEntity>().eq("is_delete",0));
		if(!CollectionUtils.isEmpty(isGroupArrayList)){
			for (Long orgId:isGroupArrayList){
				IsGroupEntity entity=new IsGroupEntity();
				entity.setOrgId(orgId);
				String orgNameById = jobController.getOrgNameById(orgId);
				entity.setOrgName(orgNameById);
				isGroupDao.insert(entity);
			}
		}
		return R.ok();
	}
	@GetMapping("/getIsGroupList")
	@ApiOperation(value = "查询是否集团公司数据列表")
	@Transactional
	public R getIsGroupList( HttpServletRequest request) throws Exception {
	 List<IsGroupEntity> isGroupEntityList=	isGroupDao.selectList(new QueryWrapper<IsGroupEntity>().eq("is_delete",0));
	 return R.ok(isGroupEntityList);
	}



}

